$(function () {
    $('form').parsley();
    dynamicBinding();
    getAllDisaster();
});

//遍历所有disaster输出到表格
function getAllDisaster() {
    let deleteStr = "";
    let updateStr = "";
    let columns = [
        {
            title: "序号", data: null, "render": function (data, type, row, meta) {
                return data = Number(meta.row) + Number(1);
            }
        },
        {title: "备案信息", data: "disasterName"},
        {title: "灾情类型", data: "disasterType"},
        {title: "发生地点", data: "occurredPosition"},
        {title: "发布时间", data: "issueTime"},
        {title: "伤亡情况", data: "casualties"},
        { title: "详细文件", data: "href", "render": function (data, type, row, meta) {
                let index = data.lastIndexOf("/");
                return '<a href="' + data + '" download="' + data.substring(index + 1, data.length) + '" target="_blank">' + data.substring(index + 1, data.length) + '</a>';
            }},
        {
            title: "操作", data: "id", "orderable": false, // 禁用排序
            "defaultContent": "", "render": function (data, type, row, meta) {
                if (hasDeletePermission) {
                    deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>&emsp;"
                }
                if (hasUpdatePermission) {
                    updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updateDisasterModal'>更新</button>";
                }
                return data = deleteStr + updateStr;
            }
        }
    ];
    let url = 'disaster/getAllDisaster';
    dataTableInit(url, columns);
}

//动态绑定
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    deleteBind('/disaster/deleteDisaster');
    //为所有的update按钮绑定update事件

    $("#datatable").on('click', ".data-update", function () {
        let data_id = $(this).attr('data-id');
        //获取此行数据
        let row = $("#datatable").DataTable().row($(this).parents('tr')).data();
        //遍历更新表单的所有拥有name的输入框，将行数据填充到这些输入框中，file除外
        $('#toUpdateDisaster'+" [name]").each(function () {
            if($(this).attr('type')!=="file"){
                let name =  $(this).attr("name");
                $(this).val(row[name]);
            }
        });
        $('#toUpdateDisaster').parsley().on('form:validate', function () {
        }).on('form:submit', function () {
            let id = data_id;
            let data = new FormData($('#toUpdateDisaster')[0]);
            data.set('issueTime', Date($(" #updateIssueTime ").val()));
            data.set("id", id);
            updateAjax(data, '/disaster/updateDisaster', false, false, "#updateDisasterModal");
            return false;//阻止默认表单提交
        });
    });
    //为所有的add按钮绑定add事件
    $('#toAddDisaster').parsley().on('form:validate', function () {
    }).on('form:submit', function () {
        let data = new FormData($('#toAddDisaster')[0]);
        data.set('issueTime', Date($(" #addIssueTime ").val()));
        addAjax(data, '/disaster/addDisaster', false, false, "#addDisasterModal");
        return false;
    });
    //addBind("#addSubmitBtn", '#toAddDisaster', '/plan/addDisaster', false, false, "#addDisasterModal");

    //为add模态框绑定消失事件-清空模态框中内容
    $('#addDisasterModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddDisaster").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updateDisasterModal').on('hidden.bs.modal', function () {
        $("#toUpdateDisaster")[0].reset();
    });


    /*$("#disaster-table-body").on('click', ".data-update", function(){
        console.log("updateDisaster被点击");
        let _id=$(this).attr('data-id');

        $("#toUpdateDisaster").ajaxForm(function () {
            let id = _id;
            let disasterName = $(" #updateDisasterName ").val();
            let disasterType = $(" #updateDisasterType ").val();
            let occurredPosition = $(" #updateOccurredPosition ").val();
            let issueTime = new Date($(" #updateIssueTime ").val());
            let casualties = $(" #updateCasualties ").val();
            let href = $(" #updateHref ").val();
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/disaster/updateDisaster',
                data: { id: id,disasterName:disasterName,disasterType:disasterType,
                         occurredPosition:occurredPosition,issueTime:issueTime,casualties:casualties,href:href},
                success: function (data) {
                    if (data.code==200){
                        window.location.reload();
                    }else {
                        console.log(data.msg);
                    }
                },
                error: function (data) {
                }
            })
        });
    });
    //为add按钮绑定add事件
    $("#toAddDisaster").on('click', function(){
        $("#toAddDisaster").ajaxForm(function () {
            let disasterName = $(" #addDisasterName ").val();
            let disasterType = $(" #addDisasterType ").val();
            let occurredPosition = $(" #addOccurredPosition ").val();
            let issueTime = new Date($(" #addIssueTime ").val());
            let casualties = $(" #addCasualties ").val();
            let href = $(" #addHref ").val();
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/disaster/addDisaster',
                data: { disasterName: disasterName,disasterType:disasterType,occurredPosition:occurredPosition,issueTime:issueTime,casualties:casualties,href:href},
                success: function (data) {
                    if(data.code==200){
                        window.location.reload();
                    }else{
                        console.log(data.msg);
                    }
                },
                error: function (data) {
                    console.log(data);
                }
            })
        });
    });*/
}
